package com.seanLab.web;

import com.seanLab.domain.Image;
import com.seanLab.dto.MetricsDto;
import com.seanLab.dto.TestDocsDto;
import com.seanLab.dto.UserDto;
import com.seanLab.service.DataService;
import com.seanLab.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据服务接口，主要使用loadImagesToES及loadSpecialImageToES
 * Created by ailly on 17-10-9.
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/v1/data")
public class DataController {
    @Autowired
    private DataService dataService;

    @Autowired
    private TestService testService;

    /**
     * 同步mongodb中image表至ES
     */
    @RequestMapping(path = "/loadToES", method = RequestMethod.GET)
    public int loadImagesToES() {
        return this.dataService.loadImagesToES();
    }

    /**
     * 同步mongodb中specialImage表至ES
     */
    @RequestMapping(path = "/loadSpecialImageToES", method = RequestMethod.GET)
    public int loadSpecialImageToES() {
        return this.dataService.loadSpecialImageToES();
    }

    //从已有文章表中抽取标签，添加至已有图片表中
    @RequestMapping(path = "/extractImgTagFromExistArticle", method = RequestMethod.GET)
    public int extractImgTagsFromExistArticleTest() {
        this.dataService.extractImgTagsFromExistArticleTest();
        return 0;
    }

    //推荐记录指标统计
    @RequestMapping(path = "/getMetrics", method = RequestMethod.GET)
    public MetricsDto getMetrics() {
        return this.dataService.getMetrics();
    }

    @RequestMapping(path = "/getReport", method = RequestMethod.GET)
    public MetricsDto getReport() {
        return this.dataService.generateReport();
    }

    @RequestMapping(path = "/generateTopTopics", method = RequestMethod.GET)
    public int generateTopTopics() {
        return this.dataService.generateTopTopics();
    }

    @RequestMapping(path = "/generateTestDocs", method = RequestMethod.GET)
    public void generateTestDocs() {
        this.testService.generateTestDocs();
    }

    @RequestMapping(path = "/getNextDoc", method = RequestMethod.POST)
    public TestDocsDto getNextDoc(@RequestBody UserDto userDto) {
        return this.testService.getNextDoc(userDto.getTestID());
    }

    @RequestMapping(path = "/loadSpecialImage", method = RequestMethod.GET)
    public int loadSpecialImage() {
        return this.dataService.loadSpecialImage();
    }

    /**
     * 仅做测试使用
     */
    @RequestMapping(path = "/testBulk", method = RequestMethod.GET)
    public String testBulk() {
        List<Image> images = new ArrayList<>();
        Image image = new Image();
        image.setImgID("imageID-705ed692-9ec2-4a95-a62f-54615e2bcd2c");
        image.setEsFlag(2);
        Image image1 = new Image();
        image1.setImgID("imageID-7bb264cb-263b-4e87-b6fe-d05602c96251");
        image1.setEsFlag(2);
        images.add(image);
        images.add(image1);
        return this.dataService.bulkUpdateEsFlag("zzztest", images);
    }
}
